package com.example.deptservicehystrix8001.controller;


import com.example.deptservicehystrix8001.service.DeptService;
import com.example.api.model.Dept;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class DeptController {

    @Autowired
    private DeptService service;


    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = service.get(id);
        if (null == dept){
            throw new RuntimeException("抛出异常");
        }
        return dept;
    }

    //备选方法 熔断返回
    public Dept hystrixGet(@PathVariable("id") Long id)
    {
        return new Dept().setDeptno(id).setDname("该ID：" + id + "没有对应的信息,null")
                .setDb_source("no this database in MySQL");
    }
}
